package com.zds.hj.poseidon.biz.common;

import com.zds.boot.common.exception.BusinessException;
import com.zds.component.files.facade.api.FilesDubboInterface;
import com.zds.component.files.facade.dto.FileAuthDto;
import com.zds.component.files.facade.dto.FilesResult;
import com.zds.component.files.facade.dto.FilesUploadReqDto;
import com.zds.zly.common.constant.CommConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @Author duanhao
 * @Description 文件服务接口
 * @Date 2023/6/8 15:03
 */
@Slf4j
@Service
public class FileServiceInterface {

    @DubboReference(owner = CommConstant.DUBBO_OWNER, version = CommConstant.DUBBO_VERSION)
    private FilesDubboInterface filesDubboInterface;



    /**
     * 上传文件
     *
     * @param bytes    文件byte数组
     * @param fileName 文件名
     * @param authDto  文件权限
     * @return
     */
    public FilesResult put(byte[] bytes, String fileName, FileAuthDto authDto) {
        FilesUploadReqDto reqDto = new FilesUploadReqDto();
        reqDto.setByteData(bytes);
        reqDto.setFileName(fileName);
        reqDto.setFileAuthDto(authDto);
        return filesDubboInterface.put(reqDto);
    }


    /**
     * 追加文件访问权限
     * @param fileAuthDto
     */
    public void addFilesAuth(FileAuthDto fileAuthDto) {
        try {
            filesDubboInterface.addFilesAuth(fileAuthDto);
        } catch (BusinessException e) {
            log.info("追加文件访问权限 异常", e);
            throw e;
        } catch (Exception ex) {
            log.info("追加文件访问权限 异常", ex);
            throw new BusinessException("调用文件服务异常", HjResultCodeEnum.SERVER_ERR.code());
        }
    }

}
